<?php

// Loading dependencies
require_once "Exceptions/PokaYokeException.class.php";
require_once "PokaYokeRetriever.class.php";


class PokaYokeDataHolder
{
	public static $GENERIC_FILTER = "PokaYokeString";


	protected $_value;


	function __construct( $value )
	{
		$this->_value = $value;
	}
	
	
	// __toString Implementation
	
	function __toString()
	{
		//throw new PokaYokeException(  --> PHP do not allow to throw exceptions inside
		// the magic method __toString due to compiler unexpected behavior.
		trigger_error(
			"Cannot access value directly. Use raw() method to retrieve original value",
			E_USER_ERROR
		);
	}

	
	// Accessor Implementation
	
	public function getValue()
	{
		return $this->_value;
	}
	
	
	public function raw()
	{
		return $this->getValue();
	}
	
	
	public function retrieve( $filterClass, $options = array() )
	{
		// Check if filterClass is already loaded
		if ( ! class_exists( $filterClass ) ) {
			$filtersDirectory = dirname( __FILE__ ) . "/Retrievers/";
			$filterFile =  $filterClass . ".retriever.php";

			if ( ! file_exists( $filtersDirectory . $filterFile ) ) {
                $filterClass = PokaYokeDataHolder::$GENERIC_FILTER;
				$filterFile =  $filterClass . ".retriever.php";
			}

			require_once $filtersDirectory . $filterFile;
		}
		
		$filter = new $filterClass( $this->_value );

		return $filter->retrieve( $options );
	}

	
	// Native Filters Accessors Implementation

	public function asInteger( $options = array() )
	{
		return $this->retrieve( "PokaYokeInteger", $options );
	}


	public function asFloat( $options = array() )
	{
		return $this->retrieve( "PokaYokeFloat", $options );
	}


	public function asHex( $options = array() )
	{
		return $this->retrieve( "PokaYokeHex", $options );
	}


	public function asString( $options = array() )
	{
		return $this->retrieve( "PokaYokeString", $options );
	}


	public function asHtml( $options = array() )
	{
		return $this->retrieve( "PokeYokeHtml", $options );
	}

	/* Not yet completed!
	public function asRegExp( $regexp, $options = array() )
	{
		$options['regexp'] = $regexp;

		return $this->filter( "PokeYokeRegExp", $options );
	}
	*/
}

?>